package top.moonkit.rs_pro.controller;

import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.moonkit.rs_pro.common.Result;
import top.moonkit.rs_pro.entity.User;
import top.moonkit.rs_pro.entity.UserToken;
import top.moonkit.rs_pro.entity.vo.RegisterRequestVO;
import top.moonkit.rs_pro.service.UserService;
import top.moonkit.rs_pro.utils.Au;
import top.moonkit.rs_pro.utils.Mu;

import java.util.HashMap;

/**
 * @author moon
 * @since
 */
// AuthController.java
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<?> registerUser(@Valid @RequestBody RegisterRequestVO registerRequest) {

        User user = userService.registerUser(registerRequest);
        return Result.success(Mu.m("person.register_success"), user);

    }


    @GetMapping("/check-email")
    public Result<?> checkEmail(@RequestParam String email) {
        boolean exists = userService.checkEmailExists(email);
        HashMap<String, Boolean> result = new HashMap<>();
        result.put("exists", exists);
        return Result.success("查询成功", result);
    }

    /**
     * 通过token获取用户信息
     */
    @GetMapping("/get-user-info")
    public Result<User> getUserInfo() {
        UserToken userToken = Au.checkAndGetToken();
        User user = userService.getById(userToken.getUserId());
        return Result.success(user);
    }
}
